package com.railway.common.util;

import java.util.List;

import com.railway.news.model.Category;
import com.railway.news.model.News;

public class CaJosn {
	 
	public CaJosn(){
		
	}
    public static String CaToJosn(Category category){
    	StringBuffer result=new StringBuffer();
    	result.append("{");
    	result.append("\"id\":").append(getJosnValue(category.getId())).append(",");
    	result.append("\"publicType\":").append(getJosnValue(category.getPublicType())).append(",");
    	result.append("\"names\":").append(getJosnValue(category.getNames())).append(",");
    	result.append("\"caTypes\":").append(getJosnValue(category.getCaTypes())).append(",");
    	result.append("\"createTimeString\":").append(getJosnValue(category.getCreateTimeString())).append(",");
    	result.append("\"newsList\":");
    	   if(category.getNewsList()!=null){
    		   result.append("[");
    		   List<News> newsList=category.getNewsList();
    		   for(int i=0;i<newsList.size();i++){
    			   News news=newsList.get(i);
    			   result.append("{");
    			   result.append("\"id\":").append(getJosnValue(news.getId())).append(",");
    			   result.append("\"title\":").append(getJosnValue(news.getTitle())).append(",");
    			   result.append("\"fromUrl\":").append(getJosnValue(news.getFromUrl())).append(",");
    			   result.append("\"createTimeString\":").append(getJosnValue(news.getCreateTimeString()));
    			   result.append("}");
    			   if(i<newsList.size()-1){
    				   result.append(",");
    			   }
    		   }
    		   result.append("]");
    	   }else{
    		   result.append(category.getNewsList()); 
    	   }
    	   result.append("}");
      return result.toString();
    }
    public static String getJosnValue(String value){
    	 if(value==null){
    		 return null;
    	 }else{
    		 return "\""+value+"\"";
    	 }
    }
}
